package com.cjt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cjt.common.dto.UserListCollegeSub;
import com.cjt.common.dto.UserListCollegeInfo;
import com.cjt.common.vo.UserEditSubjectVO;
import com.cjt.common.vo.UserListTeacher;
import com.cjt.pojo.*;
import com.cjt.service.*;
import com.cjt.mapper.UserMapper;
import com.cjt.utils.RedisUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author Hello
* @description 针对表【user】的数据库操作Service实现
* @createDate 2023-02-24 23:26:29
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Autowired
    RoleService roleService;
    @Autowired
    UserMapper userMapper;
    @Autowired
    MenuService menuService;
    @Autowired
    RedisUtil redisUtil;



    @Autowired
    CollegeSubService collegeSubService;
    @Autowired
    SubjetService subjetService;
    @Autowired
    TeacherService teacherService;
    @Autowired
    StuSubjetService stuSubjetService;
    @Autowired
    CollegeService collegeService;
    @Autowired
    CollegeTeaService collegeTeaService;

    @Override
    public User getByUsername(String username) {
        return getOne(new QueryWrapper<User>().eq("username",username));
    }

    @Override
    public String getUserAuthorityInfo(Long userId) {
        String authority = "";
        String redis = (String) redisUtil.get("GrantedAuthority:" + userId);
        if (StringUtils.isBlank(redis)) {
            //获取角色编码
            List<Role> roleList = roleService.list(new QueryWrapper<Role>().inSql("id", "select role_id from sys_user_role where user_id=" + userId));

            if (roleList.size() > 0) {
                for (Role role : roleList) {
                    String code = "ROLE_" + role.getCode();
                    authority = authority.concat(code + ",");
                }
            }

            //获取菜单操作编码
            List<Long> menuIds = userMapper.getNavMenuIds(userId);
            if (menuIds.size() > 0) {
                List<Menu> menus = menuService.listByIds(menuIds);
                for (Menu menu : menus) {
                    String menuPerms = menu.getPerms();
                    authority = authority.concat(menuPerms + ",");
                }
            }

            redisUtil.set("GrantedAuthority:" + userId, authority, 60 * 60);

            System.out.println(userId+"authority："+authority);
        } else {
            authority = redis;
        }
        return authority;

    }

    @Override
    public List<UserListCollegeSub> getListSub(Long collegeId) {
        UserListCollegeSub userListCollegeSub = new UserListCollegeSub();
        List<UserListCollegeSub> userListCollegeSubs = new ArrayList<>();

        List<CollegeSub> list = collegeSubService.list(new LambdaQueryWrapper<CollegeSub>().eq(CollegeSub::getCollegeId, collegeId));
        for (CollegeSub collegeSub : list) {

            Subjet subjet = subjetService.getById(collegeSub.getSubId());
            int count = stuSubjetService.count(new LambdaQueryWrapper<StuSubjet>().eq(StuSubjet::getSubId, subjet.getId()));
            Teacher teacher = teacherService.getOne(new LambdaQueryWrapper<Teacher>().eq(Teacher::getTeaId, subjet.getTeaId()));
            userListCollegeSub.setId(subjet.getId());
            userListCollegeSub.setName(subjet.getName());
            userListCollegeSub.setTeaName(teacher.getName());
            userListCollegeSub.setUnm(count);
            UserListCollegeSub new1 = BeanUtil.copyProperties(userListCollegeSub, UserListCollegeSub.class);
            userListCollegeSubs.add(new1);
        }
        return userListCollegeSubs;
    }

    @Override
    public List<UserListCollegeInfo> listCollege() {
        List<College> list = collegeService.list();
        UserListCollegeInfo userListCollegeInfo = new UserListCollegeInfo();
        List<UserListCollegeInfo> list1 = new ArrayList<>();
        for (College college : list) {
            userListCollegeInfo.setId(college.getId());
            userListCollegeInfo.setName(college.getName());
            UserListCollegeInfo news = BeanUtil.copyProperties(userListCollegeInfo, UserListCollegeInfo.class);
            list1.add(news);
        }
        return list1;
    }

    @Override
    public List<UserListTeacher> listTea(Long id) {
        List<CollegeTea> list = collegeTeaService.list(new LambdaQueryWrapper<CollegeTea>().eq(CollegeTea::getCollegeId, id));
        List<UserListTeacher> listTeachers = new ArrayList<>();
        for (CollegeTea collegeTea : list) {
            UserListTeacher userListTeacher = new UserListTeacher();
            Teacher teacherServiceOne = teacherService.getOne(new LambdaQueryWrapper<Teacher>().eq(Teacher::getTeaId, collegeTea.getTeaId()));
            userListTeacher.setName(teacherServiceOne.getName());
            userListTeacher.setId(teacherServiceOne.getTeaId());
            listTeachers.add(userListTeacher);
        }


        return listTeachers;
    }

    @Override
    public void saveSUb(UserEditSubjectVO userEditSubjectVO) {
        Subjet subjet = new Subjet();
        subjet.setName(userEditSubjectVO.getName());
        subjet.setTeaId(userEditSubjectVO.getTeaid());
        subjetService.save(subjet);
        CollegeSub collegeSub = new CollegeSub();
        collegeSub.setCollegeId(userEditSubjectVO.getId());
        collegeSub.setSubId(subjet.getId());
        collegeSubService.save(collegeSub);
    }


    @Override
    public void editSUb(Subjet subjet) {
        subjetService.updateById(subjet);
    }

    @Override
    public void delSub(UserEditSubjectVO userEditSubjectVO) {
        subjetService.removeById(userEditSubjectVO.getSubid());
        collegeSubService.remove(new LambdaQueryWrapper<CollegeSub>().eq(CollegeSub::getCollegeId, userEditSubjectVO.getId()));
    }
}




